#include "myhead.h"

int main()
{
	int semid;
	struct sembuf mybuf;
	bzero(&mybuf,sizeof(mybuf));
	mybuf.sem_num=0;  //信号量的序号
	mybuf.sem_op=2;  //我想对信号量做减法
	mybuf.sem_flg=SEM_UNDO;
	
	//创建一个信号量
	semid=semget(7454,1,IPC_CREAT|IPC_EXCL|0777);
	if(semid==-1)
	{
		if(errno==EEXIST)
			semid=semget(7454,1,0777);
		else
		{
			perror("创建信号量失败");
			return -1;
		}
	}
	
	//给刚才申请的那个信号量赋值
	semctl(semid,0,SETVAL,3);  //我把信号量的值设置为3
	//打印刚才信号量的值
	printf("信号量值为:%d\n",semctl(semid,0,GETVAL));
	
	//对这个信号进行减法操作
	semop(semid,&mybuf,1);
	printf("做了一次减法信号量值为:%d\n",semctl(semid,0,GETVAL));
	
	//对这个信号进行减法操作
	semop(semid,&mybuf,1);
	printf("做了一次减法信号量值为:%d\n",semctl(semid,0,GETVAL));
	
	//对这个信号进行减法操作
	semop(semid,&mybuf,1);
	printf("做了一次减法信号量值为:%d\n",semctl(semid,0,GETVAL));
	
	
	//对这个信号进行减法操作
	semop(semid,&mybuf,1);
	printf("做了一次减法信号量值为:%d\n",semctl(semid,0,GETVAL));
}